package com.skytech.tax.customsdatawarehouse.electronicport.customsdeclarations.resource;

import com.skytech.tax.customsdatawarehouse.electronicport.customsdeclarations.domain.entity.CustomsDeclarationSimple;
import com.skytech.tax.customsdatawarehouse.electronicport.customsdeclarations.domain.service.CustomsDeclarationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lianhong
 * @since 2021/6/8
 * 电子口岸 - 出口关单资源服务
 */
@RestController("electronicPort.customsDeclarationResource")
@RequestMapping("/e-port/customs-declaration")
public class CustomsDeclarationResource {

    @Resource(name = "electronicPort.customsDeclarationService")
    private CustomsDeclarationService customsDeclarationService;

    /**
     * 查询出口关单列表
     *
     * @return 进出口关单列表
     */
    @GetMapping
    public String customsDeclarations() {
        return customsDeclarationService.getCustomsDeclarations();
    }

    /**
     * 根据报关单号查询出口关单列表
     *
     * @return 进出口关单列表
     */
    @PutMapping
    public String customsDeclarations(@RequestBody List<String> entryIds) {
        return customsDeclarationService.getCustomsDeclarations(entryIds);
    }


    /**
     * 保存出口报关单信息
     *
     * @param declaration 进出口报关单
     * @return 进出口报关单
     */
    @PostMapping
    public String saveCustomsDeclaration(@RequestBody String declaration) {
        return customsDeclarationService.saveCustomsDeclaration(declaration);
    }

    /**
     * 查询报关单信息
     *
     * @param entryId 出口报关单号
     * @return 出口报关单
     */
    @GetMapping("{entryId}")
    public String customsDeclaration(@PathVariable String entryId) {
        return customsDeclarationService.getCustomsDeclaration(entryId);
    }

    /**
     * 删除报关单信息
     *
     * @param entryId 出口报关单号
     */
    @DeleteMapping("{entryId}")
    public void deleteCustomsDeclaration(@PathVariable String entryId) {
        customsDeclarationService.deleteByEntryId(entryId);
    }

    /**
     * 判断是否存在出口报关单
     *
     * @return entryId-01,entryId-02,...
     */
    @PostMapping("exists")
    public String existsCustomsDeclarations(@RequestBody List<CustomsDeclarationSimple> simpleList) {
        return customsDeclarationService.existsCustomsDeclarations(simpleList);
    }

    /**
     * 更新出口报关单
     *
     * @param entryId 出口报关单号
     * @param request 更新的数据 {"flagDec": "3", "opMode": "MOD",}
     */
    @PutMapping("{entryId}")
    public void updateCustomsDeclaration(@PathVariable String entryId, @RequestBody String request) {
        customsDeclarationService.updateCustomsDeclaration(entryId, request);
    }

    /**
     * 判断是否存在出口报关单
     *
     * @param entryId 出口报关单号
     * @return 是否存在
     */
    @GetMapping("{entryId}/exists")
    public boolean existsCustomsDeclaration(@PathVariable String entryId,
                                            @RequestParam(value = "iEDate", required = false) String iEDate) {
        return customsDeclarationService.existsCustomsDeclaration(entryId, iEDate);
    }


    /**
     * 查询报关单文件名
     *
     * @param entryId 出口报关单号
     * @return 阿里云存储文件名
     */
    @GetMapping("{entryId}/file")
    public String customsDeclarationFile(@PathVariable String entryId) {
        return customsDeclarationService.customsDeclarationFile(entryId);
    }


    /**
     * 删除报关单文件名
     *
     * @param entryId 出口报关单号
     */
    @DeleteMapping("{entryId}/file")
    public void deleteCustomsDeclarationFile(@PathVariable String entryId) {
         customsDeclarationService.deleteCustomsDeclarationFile(entryId);
    }

    /**
     * 保存报关单文件名
     *
     * @param entryId    出口报关单号
     * @param storedFile 存储文件名称
     */
    @PostMapping("{entryId}/file")
    public void saveCustomsDeclarationFile(@PathVariable String entryId, @RequestBody String storedFile) {
        customsDeclarationService.saveCustomsDeclarationFile(entryId, storedFile);
    }

    /**
     * 报关单文件是否已存储
     *
     * @param entryId 出口报关单号
     */
    @GetMapping("{entryId}/file/stored")
    public boolean customsDeclarationFileStored(@PathVariable String entryId) {
        return customsDeclarationService.customsDeclarationFileStored(entryId);
    }

}
